import ddddocr
import requests
from io import BytesIO
from PIL import Image

# 图片的URL
url = 'https://zxr-api.liuchangyunlian.com/admin-api/system/open/official/captcha?time=1732690335259'

# 设置请求头
headers = {
    'Accept': 'image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8',
    'Accept-Language': 'zh-CN,zh;q=0.9,en-GB;q=0.8,en;q=0.7,en-US;q=0.6',
    'Connection': 'keep-alive',
    'Referer': 'https://www.sanzhiyang.cn/',
    'Sec-Fetch-Dest': 'image',
    'Sec-Fetch-Mode': 'no-cors',
    'Sec-Fetch-Site': 'cross-site',
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
    'sec-ch-ua': '"Google Chrome";v="131", "Chromium";v="131", "Not_A Brand";v="24"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"macOS"'
}
ocr = ddddocr.DdddOcr(show_ad=False)  # 切换为第二套ocr模型
# 发送HTTP请求获取图片数据
response = requests.get(url, headers=headers)
if response.status_code == 200:
    # 使用BytesIO处理内存中的图片数据
    image_data = response.content

    # 将图片数据保存到本地文件
    with open('captcha_image.png', 'wb') as f:
        f.write(image_data)

    # 可选：使用PIL库打开并显示图片
    image = Image.open(BytesIO(image_data))
    result = ocr.classification(image)
    print(result)
else:
    print(f"无法下载图片，状态码: {response.status_code}")